/***************************************************************************
 * Copyright (C) 2008 Global Biodiversity Information Facility Secretariat.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 ***************************************************************************/
package org.gbif.agi.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * A high speed join table for Genus to Taxon using nested sets 
 * @author trobertson
 */
@Entity
@Table(name="genus_taxon")
public class GenusTaxon implements Comparable<Object> {
	private static final long serialVersionUID = -3738514096510977475L;
	
	private Long id;
	private Genus genus;
	private long agiDatasourceId;
	private long taxonLft;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(columnDefinition="int")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(nullable=false, name="datasource_id")
	public long getAgiDatasourceId() {
		return agiDatasourceId;
	}
	public void setAgiDatasourceId(long agiDatasourceId) {
		this.agiDatasourceId = agiDatasourceId;
	}
	
	@Column(nullable=false, name="taxon_lft")
	public long getTaxonLft() {
		return taxonLft;
	}
	public void setTaxonLft(long taxonLft) {
		this.taxonLft = taxonLft;
	}
	
	@ManyToOne
	@JoinColumn (name="genus_id")
	public Genus getGenus() {
		return genus;
	}
	public void setGenus(Genus genus) {
		this.genus = genus;
	}
	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		GenusTaxon myClass = (GenusTaxon) object;
		return new CompareToBuilder().append(this.agiDatasourceId,
				myClass.agiDatasourceId)
				.append(this.taxonLft, myClass.taxonLft).append(this.genus,
						myClass.genus).append(this.id, myClass.id)
				.toComparison();
	}
	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof GenusTaxon)) {
			return false;
		}
		GenusTaxon rhs = (GenusTaxon) object;
		return new EqualsBuilder().append(this.agiDatasourceId,
				rhs.agiDatasourceId).append(this.taxonLft, rhs.taxonLft)
				.append(this.genus, rhs.genus).append(this.id, rhs.id)
				.isEquals();
	}
	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-1594224467, 1918833829).append(
				this.agiDatasourceId).append(this.taxonLft).append(this.genus)
				.append(this.id).toHashCode();
	}
	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("id", this.id).append(
				"taxonLft", this.taxonLft).append("agiDatasourceId",
				this.agiDatasourceId).append("genus", this.genus).toString();
	}	
}
